package com.pig4cloud.pig.module.controller.wx;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.pig4cloud.pig.common.core.util.R;
import com.pig4cloud.pig.common.log.annotation.SysLog;
import com.pig4cloud.pig.common.mybatis.page.PageUtils;
import com.pig4cloud.pig.common.mybatis.page.TableDataInfo;
import com.pig4cloud.pig.module.api.entity.wx.WxLiveRoom;
import com.pig4cloud.pig.module.service.wx.IWxLiveRoomService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 微信直播房间Controller
 *
 * @author ruoyi
 * @date 2022-11-08
 */
@Validated
@Api(value = "微信直播房间控制器", tags = {"微信直播房间管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/live/room")
public class WxLiveRoomController {
    private final IWxLiveRoomService wxLiveRoomService;
    @Value("${ruoyi.imgUrl}")
    private String imgUrl;
    /**
     * 查询微信直播房间列表
     */
    @ApiOperation("查询微信直播房间列表")
    @PreAuthorize("@pms.hasPermission('module:liveRoom:list')")
    @GetMapping("/list")
    public TableDataInfo<WxLiveRoom> list(@Validated WxLiveRoom wxLiveRoom) {
        QueryWrapper<WxLiveRoom> qw = new QueryWrapper<>();

        TableDataInfo<WxLiveRoom> td = PageUtils.buildDataInfo(wxLiveRoomService.page(PageUtils.buildPage(),qw));
        List<WxLiveRoom> list = td.getData();
        for (WxLiveRoom list1:list){
            list1.setBgImgPath(imgUrl+list1.getBgImg());
        }
        return td;
    }

    /**
     * 导出微信直播房间列表
     */
    @ApiOperation("导出微信直播房间列表")
    @PreAuthorize("@pms.hasPermission('module:liveRoom:export')")
    @SysLog("微信直播房间")
    @GetMapping("/export")
    public List export(@Validated WxLiveRoom wxLiveRoom) {
        List<WxLiveRoom> list = wxLiveRoomService.list();
        // ExcelUtil<WxLiveRoom> util = new // ExcelUtil<WxLiveRoom>(WxLiveRoom.class);
        return list;// util.exportExcel(list, "微信直播房间");
    }

    /**
     * 获取微信直播房间详细信息
     */
    @ApiOperation("获取微信直播房间详细信息")
    @PreAuthorize("@pms.hasPermission('module:liveRoom:query')")
    @GetMapping("/{id}")
    public R<WxLiveRoom> getInfo(@NotNull(message = "主键不能为空") @PathVariable("id") Long id) {
        WxLiveRoom wxLiveRoom = wxLiveRoomService.getById(id);
        return R.ok(wxLiveRoom);
    }

    /**
     * 新增微信直播房间
     */
    @ApiOperation("新增微信直播房间")
    @PreAuthorize("@pms.hasPermission('module:liveRoom:add')")
    @SysLog("微信直播房间")

    @PostMapping()
    public R<Void> add(@Valid @RequestBody WxLiveRoom wxLiveRoom) {
        return R.okByRow(wxLiveRoomService.save(wxLiveRoom) ? 1 : 0);
    }

    /**
     * 修改微信直播房间
     */
    @ApiOperation("修改微信直播房间")
    @PreAuthorize("@pms.hasPermission('module:liveRoom:edit')")
    @SysLog("微信直播房间")

    @PutMapping()
    public R<Void> edit(@Valid @RequestBody WxLiveRoom wxLiveRoom) {
        return R.okByRow(wxLiveRoomService.updateById(wxLiveRoom) ? 1 : 0);
    }

    /**
     * 删除微信直播房间
     */
    @ApiOperation("删除微信直播房间")
    @PreAuthorize("@pms.hasPermission('module:liveRoom:remove')")
    @SysLog("微信直播房间")
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")  @PathVariable Long[] ids) {
        return R.okByRow(wxLiveRoomService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
